<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/laypage.css">
    <script src="js/vue/vue.min.js"></script>
    <script src="js/jquery/jquery.min.js"></script>
    <script src="js/laypage/laypage.js" charset="utf-8"></script>
    <script src="js/layer/layer.js" charset="utf-8"></script>
</head>
<body>
<div id="app" class="container">

    <form class="form-inline bg-danger" role="form">
        查询条件：
        <div class="form-group">
            <label class="sr-only" for="username">用户名称</label>
            <input type="text" class="form-control" id="username" placeholder="用户名称">
        </div>
        <div class="form-group">
            <label class="sr-only" for="userage">用户年龄</label>
            <input type="text" class="form-control" id="userage" placeholder="用户年龄">
        </div>
        <button type="button" id="findUser" class="btn btn-success">查询用户</button>
        <button type="button" id="addUserBtn" class="btn btn-danger">增加用户</button>
    </form>


    <div class="table-responsive">
        <table class="table">
            <thead>
            <tr class="success">
                <td>序号</td>
                <td>TID</td>
                <td>标题</td>
                <td>内容</td>
                <td>图片</td>
                <td>操作</td>
            </tr>
            </thead>
            <tbody>
            <tr class="active" v-for="(item,index) in result">
                <td>{{index+1}}</td>
                <td>{{item.tid}}</td>
                <td>{{item.title}}</td>
                <td>{{item.context}}</td>
                <td>{{item.pic}}</td>
                <td><a href="#" @click="update(item.tid)">修改</a>&nbsp;<a href="#" @click="del(item.tid)">删除</a></td>
            </tr>
            </tbody>
        </table>
        <div id="pagenav"></div>
    </div>
</div>
<script>

    var app = new Vue({
        el: '#app',
        data: {
            result: []
        }
    });

    var getTopicList = function (curr) {
        $.ajax({
            type: "GET",
            dataType: "json",
            url: 'getcond',
            data: {
                pageSize: 5,
                pageNum: curr || 1,
                title: $("#username").val()
            },
            success: function (data) {
                app.result = data.result;
                laypage({
                    cont: 'pagenav',
                    pages: data.totalPages,
                    first: "首页",
                    last: "尾页",
                    curr: curr || 1,
                    jump: function (obj, first) {
                        if (!first) {
                            getTopicList(obj.curr);
                        }
                    }
                });
            }
        });
    };

    getTopicList();

    $("#findUser").click(function () {
        getTopicList();
    });

    $("#addUserBtn").on('click', function () {
        layer.open({
            type: 2,
            title: "发帖",
            fix: false,
            maxmin: true,
            shadeClose: true,
            area: ['1000px', '600px'],
            content: 'user_add.html',
            end: function () {
                getTopicList();
            }
        })
    });

    var update = function (tid) {
        layer.open({
            type:2,
            title:"编辑主贴",
            fix:false,
            maxmin:true,
            shadeClose:true,
            area:['1000px','600px'],
            content:'getTopic?tid='+tid,
            end:function () {
                getTopicList();
            }
        })
    };


    var del = function (tid) {
        layer.confirm("确定删除吗?", {
            btn: ['是', '否']
        }, function () {
            //是
            $.ajax({
                type: "POST",
                url: "topic_del",
                dataType: "json",
                data: {
                    tid: tid
                },
                success:function (msg) {
                    getTopicList();
                    layer.msg("已成功删除" + tid, {icon: 5});
                }
            });
        }, function () {
            //否
        });
    }

</script>
</body>
</html>